import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/register',
    component: () => import('@/views/register')
  },
  {
    path: '/layout',
    component: () => import('@/views/layout'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@/views/home')
      },
      {
        path: '/storeinfo',
        component: () => import('@/views/storeinfo')
      },
      {
        path: '/storedetail',
        component: () => import('@/views/storedetail')
      },
      {
        path: '/muyingzx',
        component: () => import('@/views/muyingzx')
      },
      {
        path: '/zixundetail',
        component: () => import('@/views/muyingzx/zixundetail.vue')
      },
      {
        path: '/shoppingcar',
        component: () => import('@/views/shoppingcar')
      },
      {
        path: '/settlement',
        component: () => import('@/views/settlement')
      },
      {
        path: '/storecollect',
        component: () => import('@/views/mycollect/storecollect.vue')
      },
      {
        path: '/zixuncollect',
        component: () => import('@/views/mycollect/zixuncollect.vue')
      },
      {
        path: '/userinfo',
        component: () => import('@/views/user/userinfo')
      },
      {
        path: '/changepassword',
        component: () => import('@/views/user/changepassword')
      },
      {
        path: '/changepic',
        component: () => import('@/views/user/userpic')
      },
      {
        path: '/myorder',
        component: () => import('@/views/user/myorder')
      },
      {
        path: '/mypublish',
        component: () => import('@/views/user/mypublish')
      },
      {
        path: '/chat',
        component: () => import('@/views/chat/onlinechat.vue')
      },
      {
        path: '/announcement_one',
        component: () => import('@/views/announcement/announcement_text.vue')
      },
      {
        path: '/announcement_two',
        component: () => import('@/views/announcement/announcement_safe.vue')
      }
    ]
  },
  // 后台管理路由
  {
    path: '/adminlayout',
    component: () => import('@/views/admin/adminlayout'),
    redirect: '/categorymanage',
    children: [
      // 分类管理
      {
        path: '/categorymanage',
        component: () => import('@/views/admin/categorymanage')
      },
      // 商品信息管理
      {
        path: '/storeinfomanage',
        component: () => import('@/views/admin/storeinfomanage')
      },
      {
        path: '/storeinfoaudit',
        component: () => import('@/views/admin/storeinfomanage/auditstoreinfo.vue')
      },
      // 轮播图信息
      {
        path: '/imagesmanage',
        component: () => import('@/views/admin/imagesmanage')
      },
      // 用户管理
      {
        path: '/usermanage',
        component: () => import('@/views/admin/usermanage')
      },
      // 个人信息管理
      {
        path: '/personalinfo',
        component: () => import('@/views/admin/adminmanage/personalInfo.vue')
      },
      // 修改密码
      {
        path: '/changeadminpassword',
        component: () => import('@/views/admin/adminmanage/changePassword.vue')
      },
      {
        path: '/articlemanage',
        component: () => import('@/views/admin/articlemanage')
      }
    ]
  }
]

// 获取原型对象push函数
const originalPush = VueRouter.prototype.push

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/register']
  const token = store.state.token
  if (token) {
    if (!store.state.userinfo.username) {
      store.dispatch('GETUSERINFO')
    }
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
